<template>
  <div class="search-bar">
    <el-form :inline="true" :model="formItem" class="form-filter" size="mini">
      <el-form-item>
        <el-form-item>
          <treeselect
            v-model="formItem.s_knowledgeTypeCode"
            :multiple="false"
            :options="availableOganizations"
            placeholder="请选择知识库类型"
            @input="search"
          />
        </el-form-item>
      </el-form-item>
      <el-form-item>
        <el-input
          v-model="formItem.s_globleSearch"
          placeholder="全局检索"
          clearable
          size="mini"
          @keyup.enter.native="search"
          @change="onGlobeSearchInputChange"
        />
      </el-form-item>
      <el-form-item>
        <el-button plain icon="el-icon-search" @click="search" />
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { tree } from '@neu/dcom-ui/api/knowledgeType'
// 定义一个查询表单原始项
const formItem = {
  s_knowledgeTypeCode: null,
  s_globleSearch: '',
  s_startTimeBegin: '',
  s_startTimeEnd: ''
}

export default {
  name: 'KnowledgeViewFilter',
  data() {
    return {
      formItem: {
        ...formItem
      },
      availableOganizations: [],
      /** 存储时间范围 */
      timeRangeCreateTimeStart: [],
      /** 时间选择器 */
      pickerOptions: {
        shortcuts: [{
          text: '最近一周',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近一个月',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近三个月',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90)
            picker.$emit('pick', [start, end])
          }
        }]
      }
    }
  },
  created() {
    tree().then(response => {
      this.availableOganizations = response.data
    })
    this.formItem.s_knowledgeTypeCode = this.$attrs.type || null
  },
  mounted() {
  },
  methods: {
    // 查询按钮点击事件处理
    search: function() {
      if (this.timeRangeCreateTimeStart && this.timeRangeCreateTimeStart.length !== 0) {
        this.formItem.s_startTimeBegin = this.timeRangeCreateTimeStart[0]
        this.formItem.s_startTimeEnd = this.timeRangeCreateTimeStart[1]
      } else {
        this.formItem.s_startTimeBegin = ''
        this.formItem.s_startTimeEnd = ''
      }
      this.$parent.filter(this.formItem)
    },
    // 重置按钮点击事件处理
    reset: function() {
      this.formItem = {
        ...formItem
      }
      this.search()
    },
    onChange: function(val) {
      this.$parent.onResize()
    },
    onGlobeSearchInputChange(value) {
      if (value === '') {
        this.search()
      }
    }
  }
}
</script>
